Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - [VB.NET] Anagrammi parole e numeri
Forum - C# / VB.NET - [VB.NET] Anagrammi parole e numeri

Avatar
oretovalley (Normal User)
Pro


Messaggi: 109
Iscritto: 12/04/2009

Segnala al moderatore
Postato alle 21:29
Mercoledì, 20/01/2010
Questo topic è stato chiuso dal moderatore

Ciao a tutti, questo mio topic, ha due scopi, prima di tutto chiarirmi un dubbio, se stampo in msgbox tutti gli anagrammi di 8 lettere, riesco a stamparli tutti 40320, ma messi su listbox si blocca il software, potrebbe essere un limite della stessa come numero di righe?
Inoltre rilascio l'algoritmo funzionante fino a 10 caratteri, max 7 per essere messi in una listbox, 10 stampati in una msgbox.

Codice sorgente - presumibilmente VB.NET

  1. Public Class Form1
  2.  
  3.     Dim lettere_parola() As Char
  4.     Dim anagrammi() As String    
  5.     Dim lettera() As Integer
  6.     Dim fattoriale As Integer
  7.     Dim trovato As Boolean
  8.  
  9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  10.  
  11.         ListBox1.Items.Clear()
  12.  
  13.         ReDim lettera(UBound(lettere_parola))
  14.  
  15.         ReDim anagrammi(fattoriale)
  16.  
  17.         Dim number As New Random
  18.  
  19.         For cont As Integer = 0 To fattoriale - 1
  20.  
  21.             For i As Integer = 0 To UBound(lettere_parola)
  22.                 trovato = False
  23.                 While trovato = False
  24.                     lettera(i) = number.Next(1, UBound(lettere_parola) + 2)
  25.                     For j As Integer = 0 To UBound(lettere_parola)
  26.                         If lettera(i) = lettera(j) And j <> i Then
  27.                             trovato = True
  28.                         End If
  29.                     Next
  30.                     If trovato = True Then
  31.                         trovato = False
  32.                     Else
  33.                         trovato = True
  34.                     End If
  35.  
  36.                 End While
  37.                 anagrammi(cont) += TextBox1.Text(lettera(i) - 1)                
  38.             Next
  39.  
  40.             trovato = False
  41.  
  42.             For i As Integer = 0 To cont
  43.                 If anagrammi(cont) = anagrammi(i) And i <> cont Then
  44.                     trovato = True
  45.                     Exit For
  46.                 End If
  47.             Next
  48.             If trovato <> True Then
  49.                 ListBox1.Items.Add(anagrammi(cont))
  50.             End If
  51.             anagrammi(cont) = ""
  52.             If trovato = True Then
  53.                 cont -= 1
  54.             End If
  55.  
  56.             For i As Integer = 0 To UBound(lettere_parola)
  57.                 lettera(i) = 0
  58.             Next
  59.         Next
  60.  
  61.  
  62.     End Sub
  63.  
  64.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  65.         Dim num_lettere As Integer = 0
  66.  
  67.         lettere_parola = TextBox1.Text.ToCharArray
  68.         For i As Integer = 0 To UBound(lettere_parola)
  69.             trovato = False
  70.             For j As Integer = 0 To UBound(lettere_parola)
  71.                 If TextBox1.Text(i) = TextBox1.Text(j) And i <> j Then
  72.                     trovato = True
  73.                 End If
  74.             Next
  75.             If trovato = False Then
  76.                 num_lettere += 1
  77.             End If
  78.         Next
  79.  
  80.         fattoriale = 1
  81.         For i As Integer = num_lettere To 1 Step -1
  82.             fattoriale *= i
  83.         Next
  84.     End Sub
  85.  
  86. End Class



spero che possiate chiarire questo mio dubbio :asd:

PM
Avatar
oretovalley (Normal User)
Pro


Messaggi: 109
Iscritto: 12/04/2009

Segnala al moderatore
Postato alle 22:30
Mercoledì, 20/01/2010
Risolto ecco l'algoritmo funzionante:

Codice sorgente - presumibilmente VB.NET

  1. Public Class Form1
  2.  
  3.     Dim lettere_parola() As Char
  4.     Dim anagrammi() As String    
  5.     Dim lettera() As Integer
  6.     Dim fattoriale As Integer
  7.     Dim trovato As Boolean
  8.  
  9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  10.  
  11.         ListBox1.Items.Clear()
  12.  
  13.         ReDim lettera(UBound(lettere_parola))
  14.  
  15.         ReDim anagrammi(fattoriale)
  16.  
  17.         Dim number As New Random
  18.  
  19.         For cont As Integer = 0 To fattoriale - 1
  20.  
  21.             For i As Integer = 0 To UBound(lettere_parola)
  22.                 trovato = False
  23.                 While trovato = False
  24.                     lettera(i) = number.Next(1, UBound(lettere_parola) + 2)
  25.                     For j As Integer = 0 To UBound(lettere_parola)
  26.                         If lettera(i) = lettera(j) And j <> i Then
  27.                             trovato = True
  28.                             Exit For
  29.                         End If
  30.                     Next
  31.                     If trovato = True Then
  32.                         trovato = False
  33.                     Else
  34.                         anagrammi(cont) += TextBox1.Text(lettera(i) - 1)
  35.                         Exit While
  36.                     End If
  37.                 End While
  38.             Next
  39.  
  40.             trovato = False
  41.  
  42.             For i As Integer = 0 To cont
  43.                 If anagrammi(cont) = anagrammi(i) And i <> cont Then
  44.                     trovato = True
  45.                     Exit For
  46.                 End If
  47.             Next
  48.             If trovato <> True Then
  49.                 ListBox1.Items.Add(anagrammi(cont))
  50.             End If
  51.             anagrammi(cont) = ""
  52.             If trovato = True Then
  53.                 cont -= 1
  54.             End If
  55.  
  56.             For i As Integer = 0 To UBound(lettere_parola)
  57.                 lettera(i) = 0
  58.             Next
  59.         Next
  60.  
  61.     End Sub
  62.  
  63.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  64.         Dim num_lettere As Integer = 0
  65.  
  66.         lettere_parola = TextBox1.Text.ToCharArray
  67.         For i As Integer = 0 To UBound(lettere_parola)
  68.             trovato = False
  69.             For j As Integer = 0 To i
  70.                 If TextBox1.Text(i) = TextBox1.Text(j) And i <> j Then
  71.                     trovato = True
  72.                 End If
  73.             Next
  74.             If trovato = False Then
  75.                 num_lettere += 1
  76.             End If
  77.         Next
  78.  
  79.         fattoriale = 1
  80.         For i As Integer = num_lettere To 1 Step -1
  81.             fattoriale *= i
  82.         Next
  83.     End Sub
  84. End Class



PM